package com.vcs.frm.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.vcs.frm.dao.common.CommonDao;
import com.vcs.frm.domain.Job;
import com.vcs.frm.web.form.js.JobSearchFormBean;

@Controller
@RequestMapping(value = "/")
public class JobSearchController {

	public final Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private CommonDao commonDao;
	
	private final List<Integer> exp = new ArrayList<Integer>();

	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}
	
	public JobSearchController() {
		for (int i = 0; i <= 40; i++) {

			exp.add(i);
		}
	}

	@RequestMapping(value = "/jobsearch", method = RequestMethod.GET)
	public ModelAndView searchJob() {
		log.info("method - searchJob");

		ModelAndView mav = new ModelAndView();

		JobSearchFormBean formBean = new JobSearchFormBean();
		
		List<Job> jobs = commonDao.getRecentJobs(4);
		
		mav.addObject("msg", "Recent Jobs");
		mav.addObject("jobs", jobs);
		mav.addObject("jobSearch", formBean);
		mav.addObject("exp", exp);
		mav.setViewName("jobsearch");
		return mav;
	}

	@RequestMapping(value = "/jobsearch", method = RequestMethod.POST)
	public ModelAndView processSearchJobsForm(
			@ModelAttribute("jobSearch") JobSearchFormBean formBean,
			HttpServletResponse response) {

		log.info("method - processSearchJobsForm");
		ModelAndView mav = new ModelAndView();
		log.info(">>>>>>>>>>>>>>>>>>>>>>>> " + formBean);

		Job job = new Job();
		job.setJobTitle("test title");
		job.setJobDescription("long job description");

		List<Job> jobs = new ArrayList<Job>();
		jobs.add(job);
		mav.addObject("jobs", jobs);
		mav.addObject("jobSearch", formBean);
		mav.addObject("exp", exp);
		mav.setViewName("jobsearch");
		return mav;

	}
}
